package wallet

import (
	"context"
	"go.uber.org/zap"
	gamePb "navi-mumbai/app/rpc/game/pb"
	"navi-mumbai/app/rpc/wallet/pb"

	"navi-mumbai/app/api/game/internal/svc"
	"navi-mumbai/app/api/game/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserWalletBalanceLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserWalletBalanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserWalletBalanceLogic {
	return &UserWalletBalanceLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserWalletBalanceLogic) UserWalletBalance(req *types.UserWalletBalanceReq) (resp *types.UserWalletBalanceResp, err error) {
	// 查询openId
	var in gamePb.GetUidByOpenIdReq
	in.OpenId = req.OpenId
	in.ProviderId = req.ProviderId
	var rsp *gamePb.GetUidByOpenIdResp
	rsp, err = l.svcCtx.GameUserRpc.GetUidByOpenId(l.ctx, &in)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, err
	}

	var in2 pb.UserWalletBalanceReq
	in2 = pb.UserWalletBalanceReq{
		Uid:       rsp.Uid,
		MoneyType: req.MoneyType,
	}

	var rsp2 *pb.UserWalletBalanceResp
	rsp2, err = l.svcCtx.WalletRpc.UserWalletBalance(l.ctx, &in2)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return &types.UserWalletBalanceResp{}, err
	}

	resp = &types.UserWalletBalanceResp{
		MoneyType: rsp2.MoneyType,
		Balance:   rsp2.Balance,
	}
	return
}
